package com.haiqi.config.webmagic;

import org.apache.log4j.Logger;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Selectable;

import java.util.List;

/**
 * Created by hythzx on 2016/8/11.
 */
//@Component
public class NewsSpider implements PageProcessor {

    private Site site = Site.me().setRetryTimes(3).setSleepTime(100);

    private Logger logger = Logger.getLogger(this.getClass());

    @Override
    public void process(Page page) {
        Selectable xpath = page.getHtml().xpath("//a/@href");
        List<String> all = xpath.all();
        System.out.println(all);
//        page.putField("readme", xpath);
    }

    @Override
    public Site getSite() {
        return site;
    }

//    @Scheduled(fixedRate = 50000)
    public void run(){
        logger.info("------------------开始调用网页蜘蛛程序抓取网页数据----------------------------");
        Spider.create(this).addUrl("http://www.dy2018.com/").thread(5).run();
        logger.info("--------------网页蜘蛛程序抓取网页完毕---------------");
    }

    public static void main(String[] args) {
        Spider.create(new NewsSpider()).addUrl("http://www.dy2018.com/").thread(5).run();
    }
}
